#!/usr/bin/python3
import neuralNet,loader,numpy as np
from matplotlib import pyplot as plt
import pdb

trainFile="../datasets/train_catvnoncat.h5"
testFile="../datasets/test_catvnoncat.h5"

trainData,trainAnswer,trainSize=loader.load(trainFile,'train_set_x','train_set_y')
testData,testAnswer,testSize=loader.load(testFile,'test_set_x','test_set_y')

trainData=trainData/255
testData=testData/255

restoreFileName=input("Restore net from file: (Leave empty to create a new one. Don't include suffix.)")
if restoreFileName=='':
    neurons=[100,200,300,1]
    net=neuralNet.neuralNet(trainData.shape[0],neurons)
else:
    net=neuralNet.neuralNet(name=restoreFileName)

#losses=net.train(trainData,trainAnswer,times,step=50)

def rightrate(net,answer):
    res=net.output.copy()
    res[res>=0.5]=1
    res[res<0.5]=0
    comp=res[res==answer]
    return comp.shape[0]/res.shape[1]
def test():
    net.layers[0].data=testData
    net.y=testAnswer
    net.forward()
    return net.loss()

for i in range(50):
    net.train(trainData,trainAnswer,100,lr=10**(-i//20))
    print('right rate is',rightrate(net,trainAnswer),'loss is',net.loss())
    print('test loss is',test(),'test right rate is',rightrate(net,testAnswer))

